package client;

import com.donovanhatch.androidClient.R;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ServerEditor extends Activity{
	
	private EditText mIpAddress = null;
    private EditText mPort = null;
    private Button mSave = null;
    private Button mBtnLoc = null;
    private EditText mLoc = null;
    private SharedPreferences settings = null;
    private Location baseLocation = null;
    private boolean locationChanged = false;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.servereditor);
		settings = getSharedPreferences("com.donovanhatch.com.androidClient",MODE_PRIVATE);	
		
		 mIpAddress = (EditText)findViewById(R.id.txtIp);
	     mPort = (EditText)findViewById(R.id.txtPort);
	     mSave = (Button)findViewById(R.id.btnSave);
	     mBtnLoc = (Button)findViewById(R.id.btnGetLoc);
	     mLoc = (EditText)findViewById(R.id.txtLoc); 
	     
	     mIpAddress.setText(settings.getString("ipAddress", "0.0.0.0"));
	     int port = settings.getInt("port", 63400);
		 mPort.setText(Integer.toString(port));
		 baseLocation = new Location(settings.getString("baseLocation", null));
		 //mLoc.setText(baseLocation.toString());
		 	 
		// Acquire a reference to the system Location Manager
		LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);	
	        
	    // Define a listener that responds to location updates
	    final LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
            	if(location.getAccuracy() <= 4828) {
            		baseLocation = location; 
            	}
            }                      
            public void onStatusChanged(String provider, int status, Bundle extras) {}
            public void onProviderEnabled(String provider) {}
            public void onProviderDisabled(String provider) {}
         };
         
         //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 0, locationListener);
                           
         mBtnLoc.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					mLoc.setText(baseLocation.toString());
					locationChanged = true;
				}
		 });
			
		 mSave.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				SharedPreferences.Editor editor = settings.edit();
				@SuppressWarnings("unused")
				String ip = null;
				if((ip = mIpAddress.getText().toString()) != ""){
					editor.putString("ipAddress", mIpAddress.getText().toString());
				}
				
				int newPort = -1;
				if((newPort = Integer.parseInt(mPort.getText().toString())) != -1){
					editor.putInt("port", newPort); 
				}
				
				if(locationChanged == true){
					editor.putString("baseLocation", baseLocation.toString());
				}
				
				editor.commit();
				finish();
			}
	     });
	 }
}
